package com.jonah.demo.hbase.thrift.config;


import com.jonah.demo.hbase.thrift.common.PropertiesUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

public class HBaseThriftConfig {

    public static final String PREFIX = "phone-md5";

    private static final String SPLITE = ",";
    private static Logger LOG = LoggerFactory.getLogger(HBaseThriftConfig.class);
    private static volatile HBaseThriftConfig config = null;


    private String startRow;
    private String endRow;
    private Long startRowLong;
    private Long endRowLong;
    private String zkHost;
    private String tableName;
    private String sig;
    private Integer batchInt;
    private String batch;
    private String thriftHost;
    private String thrift2Host;
    private String thriftPort;
    private Integer thriftPortInt;
    private String thrift2Port;
    private Integer thrift2PortInt;


//    private static List<String> list = new Arrays


    public Integer getThriftPortInt() {
        return thriftPortInt;
    }

    public void setThriftPortInt(Integer thriftPortInt) {
        this.thriftPortInt = thriftPortInt;
    }

    public Integer getThrift2PortInt() {
        return thrift2PortInt;
    }

    public void setThrift2PortInt(Integer thrift2PortInt) {
        this.thrift2PortInt = thrift2PortInt;
    }

    public String getThriftHost() {
        return thriftHost;
    }

    public void setThriftHost(String thriftHost) {
        this.thriftHost = thriftHost;
    }

    public String getThrift2Host() {
        return thrift2Host;
    }

    public void setThrift2Host(String thrift2Host) {
        this.thrift2Host = thrift2Host;
    }

    public String getThriftPort() {
        return thriftPort;
    }

    public void setThriftPort(String thriftPort) {
        this.thriftPort = thriftPort;
        if(StringUtils.isNotBlank(thriftPort)){
            this.thriftPortInt = Integer.parseInt(thriftPort);
        }
    }

    public String getThrift2Port() {
        return thrift2Port;
    }

    public void setThrift2Port(String thrift2Port) {
        this.thrift2Port = thrift2Port;
        if(StringUtils.isNotBlank(thrift2Port)){
            this.thrift2PortInt = Integer.parseInt(thrift2Port);
        }
    }

    public Integer getBatchInt() {
        return batchInt;
    }

    public void setBatchInt(Integer batchInt) {
        this.batchInt = batchInt;
    }

    public String getBatch() {
        return batch;
    }

    public void setBatch(String batch) {
        this.batch = batch;
        if (StringUtils.isNotBlank(batch)){
            this.batchInt = Integer.parseInt(batch);
        }
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public String getSig() {
        return sig;
    }

    public void setSig(String sig) {
        this.sig = sig;
    }

    public String getZkHost() {
        return zkHost;
    }

    public void setZkHost(String zkHost) {
        this.zkHost = zkHost;
    }

    public String getStartRow() {
        return startRow;
    }

    public void setStartRow(String startRow) {
        this.startRow = startRow;
        if (StringUtils.isNotBlank(startRow)){
            this.startRowLong = Long.parseLong(startRow);
        }

    }

    public String getEndRow() {
        return endRow;
    }

    public void setEndRow(String endRow) {
        this.endRow = endRow;
        if (StringUtils.isNotBlank(endRow)){
            this.endRowLong = Long.parseLong(endRow);
        }
    }

    public Long getStartRowLong() {
        return startRowLong;
    }

    public void setStartRowLong(Long startRowLong) {
        this.startRowLong = startRowLong;
    }

    public Long getEndRowLong() {
        return endRowLong;
    }

    public void setEndRowLong(Long endRowLong) {
        this.endRowLong = endRowLong;
    }

    static {
        HBaseThriftConfig.config = HBaseThriftConfig.getInstant();
    }

    public static HBaseThriftConfig getInstant() {
//        System.out.println("HBaseThriftConfig.getInstant start");
//        LOG.info("HBaseThriftConfig.getInstant start");
        if (config == null) {
            synchronized (HBaseThriftConfig.class) {
                if (config == null) {
                    try {
                        config = new HBaseThriftConfig();
                        Class<? extends HBaseThriftConfig> configClass = config.getClass();
                        Field[] fields = configClass.getDeclaredFields();
                        for (Field field : fields) {
                            int modifiers = field.getModifiers();
                            if (!Modifier.isStatic(modifiers) && !Modifier.isFinal(modifiers)) {
                                field.setAccessible(true);
                                Object value = PropertiesUtils.getValue(String.format("%s.%s", PREFIX, field.getName()));
                                if (value != null) {
                                    PropertyUtils.setProperty(config, field.getName(), value);
                                }
                            }
                        }
                    } catch (Exception e) {
                        LOG.error("HBaseThriftConfig 初始化失败", e);
                    }
//                    LOG.info("HBaseThriftConfig.getInstant end");
//                    LOG.debug("HBaseThriftConfig.getInstant values=>{}", JsonUtils.toJson(config));
                }
            }
        }
//        System.out.println(String.format("HBaseThriftConfig.getInstant start configValue => {%s}",JsonUtils.toJson(config)));
        return config;
    }


}
